function Util() {
};

Util.prototype = new Object();

/**
 * 静态方法
 * @returns
 */
Util.getRandom = function() {
	var d = new Date();
	return Math.abs(Math.sin(Math.cos(d.getTime())));
};

Util.getUrl = function(window) {
	return window.location.href;
};

Util.openDialog = function(obj) {
	var html = " <div class='modal hide fade' id='dialogWindow'>	"
			+ "   <div class='modal-header'> "
			+ "     <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button> "
			+ "     <h3>#TITLE#</h3> " + "   </div> "
			+ "   <div class='modal-body'> "
			+ "     <p style='font-family:微软雅黑;'>#CONTEXT#</p> " + "   </div> "
			+ "   <div class='modal-footer'> "
			+ "     <a href='#' class='btn closeWindow'>关闭</a> " + "   </div> "
			+ " </div> ";
	var title = obj.title || "系统提示";
	var context = obj.context || "请指定提示内容。";
	$(html.replace(/#TITLE#/g, title).replace(/#CONTEXT#/g, context)).appendTo(
			$("body"));
	$('#dialogWindow').modal().find(".closeWindow").click(function() {
		$("#dialogWindow").remove();
		if(obj.url != null) window.location.replace(obj.url);
	});
};

Util.createElement = function(tag,value) {
	var elementHtml = "<" + tag + "></" + tag + ">";
	var element = $(elementHtml);
	if(tag == "input") element.attr("type","text"); 
	element.val(value);
	return element;
};

Util.removeWorking = function() {
	var td = $(".edit-cell").parent();
	var nv = $(".edit-cell").val();
	var ov = td.attr("ov");
	if(nv != ov) {
		td.addClass("warning");
	}else{
		td.removeClass("warning");
	}
	td.text(nv);
	$(".edit-cell").remove();
};

Util.clearMark = function(){
	$(".warning").removeClass("warning");
	$(".info").removeClass("info");
	$(".edit-cell").removeClass("edit-cell");
};

Util.edit = function(td){
	Util.removeWorking();
	if(td.find("select").length > 0) return;
	if(td.attr("readonly") != "readonly"){
		var tag = td.attr("displaytype");
		var text = td.text().trim();
		td.text("");
		var element = Util.createElement(tag,text);
		element.addClass("edit-cell").appendTo(td).focus();
		
		//高亮选中行
		$(".info").removeClass("info");
		td.parent().addClass("info");
		
		//如果是键入的回车键，则：关闭此窗口，将值传递到父元素
		element.keydown(function(event){
			if(event.keyCode == 13){
				Util.removeWorking();
				$(".info").removeClass("info");
			}
		});
	} else {
		var option = {};
		option.title = "系统提示";
		option.context = "此字段不能编辑!";
		Util.openDialog(option);
	}
};

Util.getDelete = function(tr,table,key){
	var sql = "Delete " + table + " Where #KEYS#";
	var keys = key.split(",");
	if(keys.length == 0) return;
	var where = "";
	for ( var i = 0; i < keys.length; i++) {
		var k = keys[i];
		var v = tr.find("td[col='"+k+"']").text().trim();
		if(i != 0) where += "And ";
		where += (k + " = '" + v + "' ");
	}
	sql = sql.replace(/#KEYS#/g, where);
	return sql;
};

Util.getUpdate = function(tr,table,key){
	var sql = "Update " + table + " Set #COLS# Where #KEYS#";
	var keys = key.split(",");
	if(keys.length == 0) return;
	var where = "";
	var values = "";
	for ( var i = 0; i < keys.length; i++) {
		var k = keys[i];
		var v = tr.find("td[col='"+k+"']").text().trim();
		if(i != 0) where += "And ";
		where += (k + " = '" + v + "' ");
	}
	sql = sql.replace(/#KEYS#/g, where);
	
	//被更改的字段
	$.each(tr.find("td.warning"),function(i,n){
		var col = $(this).attr("col");
		var value = $(this).text().trim();
		if($(this).find("select").length > 0) value = $(this).find("select").val();
		if(i != 0) values += ",";
		values += col + "='"+value+"' ";
	});
	sql = sql.replace(/#COLS#/g, values);
	return sql;
};

Util.getInsert = function(tr,table,key){
	var sql = "Insert Into " + table + " (#COLS#) VALUES (#VALUES#)";
	var cols = [];
	var values = [];
	//被更改的字段
	$.each(tr.find(".td"),function(i,n){
		var col = $(this).attr("col");
		var value = $(this).text().trim();
		if($(this).find("select").length > 0) value = $(this).find("select").val();
		cols.push(col);
		values.push("'"+value+"'");
		
	});
	sql = sql.replace(/#COLS#/g, cols.join(","));
	sql = sql.replace(/#VALUES#/g, values.join(","));
	return sql;
};

/**
 * 需要实例的方法
 */
//Util.prototype.say = function(){
//	alert(1);
//};
